package com.zzh.springairobort.config;

import com.zzh.springairobort.function.BookingFunctionCall;
import com.zzh.springairobort.function.LocationWeatherFunctionCall;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
//import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;

import java.util.function.Function;

/**
 * @author: zzh
 * @date: 2025/04/01 22:30:38
 * @version: 1.0
 */
@Configuration
public class CommConfiguration {

    /**
     * 创建聊天记忆 基于内存Map实现 会话id->聊天记录
     * @return 聊天记忆
     */
    @Bean
    public ChatMemory chatMemory() {
        return new InMemoryChatMemory();
    }

    /**
     * 创建聊天客户端
     * @param openAiChatModel 模型
     * @param chatMemory 聊天记忆
     * @return 聊天客户端
     */
    @Bean
    public ChatClient chatClient(OpenAiChatModel openAiChatModel, ChatMemory chatMemory) {
        return ChatClient
                .builder(openAiChatModel)
                //设置角色
                .defaultSystem("你现在是一个温柔的医院智能助手，你的名字叫小团团，请以小团团的身份和语气回答问题")
                .defaultAdvisors(
                        //设置聊天记忆
                        new MessageChatMemoryAdvisor(chatMemory)
                )
                .defaultFunctions("locationWeatherFunction", "bookingFunction")//设置默认功能 这里可以设置多个功能
                .build();
    }


    //可以设置一些特定问题的回答
    @Bean(name = "locationWeatherFunction")
    @Description("某个地方的天气怎么样")
    public Function<LocationWeatherFunctionCall.Request, LocationWeatherFunctionCall.Response> locationWeatherFunction() {
        return new LocationWeatherFunctionCall();
    }

    //可以设置一些特定问题的回答
    @Bean(name = "bookingFunction")
    @Description("如何预约挂号?")
    public Function<BookingFunctionCall.Request, BookingFunctionCall.Response> bookingFunction() {
        return new BookingFunctionCall();
    }

}
